<?php

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;

abstract class Controller
{
    /**
     * 返回成功信息
     * @param array $data
     * @param string $message
     * @param int $code
     * @return JsonResponse
     */
    protected function success(array $data = [], string $message = '', int $code = 200): JsonResponse
    {
        $response = [
            'status' => 0,
            'message' => $message,
            'data' => $data
        ];
        return response()->json($response, $code);
    }

    /**
     * 返回失败信息
     * @param array $error
     * @param string $message
     * @param int $code
     * @return JsonResponse
     */
    protected function error(array $error = [], string $message = '', int $code = 404): JsonResponse
    {
        $response = [
            'status' => 1,
            'message' => $message,
            'error' => $error
        ];
        return response()->json($response, $code);
    }
}
